import {getDetail, getPageList} from "@/api/iot/category";

export const option = (safe) => {
  const validateName = (rule, value, callback) => {
    const regex = /^[a-zA-Z0-9_\-\u4e00-\u9fa5:.@]{4,32}$/;
    const isValid = regex.test(value);
    if (!isValid) {
      callback(new Error('请输入中英文，并可包含数字、下划线（_）、中划线（-）、点号（.）、半角冒号（:）以及@，整体长度需控制在4至32个字符之间'));
      return;
    }
    callback();
  };
  return {
    height: 'auto',
    calcHeight: 32,
    grid: true,
    gridSpan: 8,
    tip: false,
    searchShow: true,
    searchMenuSpan: 6,
    searchIndex: 3,
    searchIcon: true,
    border: true,
    index: true,
    viewBtn: false,
    editBtn: false,
    delBtn: false,
    selection: true,
    menuWidth: 300,
    labelWidth: 100,
    dialogType: 'drawer',
    dialogWidth: 800,
    dialogClickModal: false,
    gridBackground: (row, index) => {
      if (row.status === 1) {
        return 'linear-gradient(to bottom, rgba(88, 158, 258, 0.1), white)'
      } else {
        return 'linear-gradient(to bottom, rgba(128, 128, 128, 0.1), white)'
      }
    },
    column: [
      {
        label: "产品名称",
        prop: "productName",
        search: true,
        slot: true,
        display: false,
        gridRow: true,
      },
      {
        label: "产品编码",
        prop: "productKey",
        width: 160,
        search: true,
        display: false,
        gridRow: true,
      },
      {
        label: "产品品类",
        prop: "categoryNameFull",
        display: false,
        gridRow: true,
      },
      {
        label: "设备类型",
        prop: "deviceType",
        type: "radio",
        dicUrl: "/blade-system/dict/dictionary?code=device_type",
        props: {
          label: "dictValue",
          value: "dictKey"
        },
        value: "direct_connect",
        gridRow: true,
        search: true,
        editDisabled: true,
        display: false,
        rules: [{
          required: true,
          message: "请输入设备类型",
          trigger: "blur"
        }],
      },
      {
        label: "连接协议",
        prop: "linkProtocol",
        type: "select",
        dicUrl: "/blade-system/dict/dictionary?code=device_protocol",
        props: {
          label: "dictValue",
          value: "dictKey"
        },
        search: true,
        gridRow: true,
        editDisabled: true,
        display: false,
        rules: [{
          required: true,
          message: "请输入连接协议",
          trigger: "blur"
        }],
      },
      {
        label: "数据状态",
        prop: "status",
        type: "radio",
        dicUrl: "/blade-system/dict/dictionary?code=data_status",
        dataType: "number",
        props: {
          label: "dictValue",
          value: "dictKey"
        },
        value: 0,
        rules: [{
          required: true,
          message: "请配置数据状态",
          trigger: "blur"
        }],
        search: true,
        slot: true,
        display: false,
        width: 90,
        gridRow: true,
      },
      {
        label: "最后更新时间",
        prop: "updateTime",
        type: "input",
        display: false,
        span: 24,
        gridRow: true,
      },
      {
        label: '',
        prop: 'imgBox',
        display: false,
      },
    ],
    group: [
      {
        label: '产品信息',
        prop: 'cpxx',
        icon: 'el-icon-platform',
        column: [
          {
            label: "产品名称",
            prop: "productName",
            type: "input",
            span: 24,
            search: true,
            rules: [{
              required: true,
              trigger: "change",
              validator: validateName
            }],
          },
          {
            label: "产品编码",
            labelTip: '留空则系统自动生成12位编码',
            prop: "productKey",
            type: "input",
            span: 24,
            editDisabled: true,
          },
          {
            label: "产品密钥",
            labelTip: '留空则系统自动生成16位密钥',
            prop: "productSecret",
            type: "input",
            span: 24,
            editDisabled: true,
          },
          {
            label: "产品品类",
            labelTip: "可选择已发布的品类数据，绑定至产品并将其物模型同步",
            prop: "categoryId",
            type: "table",
            editDisabled: true,
            children: {
              border: true,
              column: [
                {
                  label: "品类ID",
                  prop: "id",
                  hide: true,
                },
                {
                  label: "品类行业",
                  prop: "categoryIndustry",
                  type: "select",
                  dicUrl: "/blade-system/dict/dictionary?code=category_industry",
                  dataType: "number",
                  props: {
                    label: "dictValue",
                    value: "dictKey"
                  },
                  search: true,
                  rules: [{
                    required: true,
                    message: "请选择品类行业",
                    trigger: "blur"
                  }],
                  span: 24,
                },
                {
                  label: "品类场景",
                  prop: "categoryScene",
                  type: "select",
                  dicUrl: "/blade-system/dict/dictionary?code=category_scene",
                  dataType: "number",
                  props: {
                    label: "dictValue",
                    value: "dictKey"
                  },
                  search: true,
                  rules: [{
                    required: true,
                    message: "请选择品类场景",
                    trigger: "blur"
                  }],
                  span: 24,
                },
                {
                  label: "品类名称",
                  prop: "categoryName",
                  search: true,
                },
                {
                  label: "排序号",
                  prop: "sort",
                }
              ],
            },
            formatter(row) {
              if (!row.id) return ''
              return row.categoryIndustryName + '/' + row.categorySceneName + "/" + row.categoryName;
            },
            onLoad: ({page, value, data}, callback) => {
              let param = {};
              let currentPage = 1;
              let pageSize = 10;

              if (value) {
                getDetail(value).then(res => {
                  callback(res.data.data);
                });
                return;
              }
              if (data) {
                param = data;
              }
              if (page) {
                currentPage = page.currentPage;
                pageSize = page.pageSize;
              }
              getPageList(currentPage, pageSize, param).then(res => {
                const data = res.data.data;
                //分页查询信息
                callback({
                  total: data.total,
                  data: data.records
                })
              });
            },
            props: {
              label: 'categoryName',
              value: 'id'
            },
            span: 24,
            slot: true,
            rules: [{
              required: true,
              message: "请选择产品关联品类",
              trigger: "change"
            }],
          },
          {
            label: "产品图标",
            prop: "logo",
            type: 'upload',
            listType: 'picture-img',
            propsHttp: {
              res: 'data',
              url: 'link',
            },
            action: '/blade-resource/oss/endpoint/put-file',
            tip: '只能上传jpg/png文件，且不超过500kb',
            row: true,
            span: 24,
          },
        ]
      },
      {
        label: '数据信息',
        prop: 'sjxx',
        icon: 'el-icon-promotion',
        column: [
          {
            label: "设备类型",
            prop: "deviceType",
            type: "radio",
            dicUrl: "/blade-system/dict/dictionary?code=device_type",
            props: {
              label: "dictValue",
              value: "dictKey"
            },
            value: "direct_connect",
            span: 24,
            search: true,
            editDisabled: true,
            rules: [{
              required: true,
              message: "请输入设备类型",
              trigger: "blur"
            }],
          },
          {
            label: "连接协议",
            prop: "linkProtocol",
            type: "select",
            dicUrl: "/blade-system/dict/dictionary?code=device_protocol",
            props: {
              label: "dictValue",
              value: "dictKey"
            },
            span: 24,
            editDisabled: true,
            rules: [{
              required: true,
              message: "请输入连接协议",
              trigger: "blur"
            }],
          },
          {
            label: "联网方式",
            prop: "connectMode",
            type: "select",
            dicUrl: "/blade-system/dict/dictionary?code=connect_mode",
            props: {
              label: "dictValue",
              value: "dictKey"
            },
            span: 24,
            editDisabled: true,
            rules: [{
              required: true,
              message: "请输入联网方式",
              trigger: "blur"
            }],
          },
          {
            label: "数据格式",
            prop: "dataType",
            type: "select",
            dicUrl: "/blade-system/dict/dictionary?code=data_type",
            props: {
              label: "dictValue",
              value: "dictKey"
            },
            span: 24,
            editDisabled: true,
            rules: [{
              required: true,
              message: "请输入数据格式",
              trigger: "blur"
            }],
          },
          {
            label: "数据状态",
            prop: "status",
            type: "radio",
            dicUrl: "/blade-system/dict/dictionary?code=data_status",
            dataType: "number",
            props: {
              label: "dictValue",
              value: "dictKey"
            },
            value: 0,
            editDisabled: true,
            rules: [{
              required: true,
              message: "请配置数据状态",
              trigger: "blur"
            }],
            slot: true,
            width: 90,
            span: 24,
          },
          {
            label: "动态注册",
            prop: "dynamicRegister",
            type: "radio",
            dicUrl: "/blade-system/dict/dictionary?code=yes_no",
            dataType: "number",
            props: {
              label: "dictValue",
              value: "dictKey"
            },
            value: 0,
            editDisabled: true,
            rules: [{
              required: true,
              message: "请配置动态注册",
              trigger: "blur"
            }],
            width: 90,
            span: 24,
          },
        ]
      },
      {
        label: '基础信息',
        prop: 'jcxx',
        icon: 'el-icon-dataline',
        column: [
          {
            label: "生产厂商",
            prop: "vendors",
            type: "input",
            span: 24,
          },
          {
            label: "产品型号",
            prop: "model",
            type: "input",
            span: 24,
          },
          {
            label: "产品描述",
            prop: "productDesc",
            type: "textarea",
            span: 24,
          },
        ]
      },
    ]
  }
}
